<?php
namespace app\common\logic;

use app\common\library\exception\ApiException;
use app\common\library\Helper\DateTime;
use app\common\library\MemberVo;
use app\common\library\Upload;
use app\common\model\Car;
use app\common\model\CarImages;
use app\common\model\MaintainRecord;
use app\common\model\MaintainRecordImage;
use app\common\model\Member;
use app\common\model\MemberApply;
use app\common\model\Order;
use app\common\model\ShopMember;
use think\Cache;
use think\db\Query;
use think\Env;


class MemberApplyLogic extends BaseLogic
{
    public $model;

    public function assignModel()
    {
        $this->model          = new MemberApply();
    }

    public function handleSearch(Query $query, array $params): Query
    {
        return $query;
    }


    /**
     * 新增记录
     * @param $params
     * @return array|bool
     * @throws ApiException
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function add($params)
    {
        //查看是否已是店铺成员
        $info = ShopMember::get(['shop_id'=>$params['shop_id'],'member_id'=>MemberVo::instance()->getMemberId(),'is_del'=>0]);
        if ($info)
            throw new ApiException('已是店铺成员，请勿重复申请',404);
        $info = MemberApply::get(['shop_id'=>$params['shop_id'],'member_id'=>MemberVo::instance()->getMemberId(),'status'=>1]);
        if ($info)
            throw new ApiException('您已在申请列表中，请勿重复申请',404);
        if (isset($params['type']) && $params['type'] == 1){
            $num = ShopMember::where(['shop_id'=>$params['shop_id'],'is_del'=>0])->count();
            if ($num == 0){
                ShopMember::create(['shop_id'=>$params['shop_id'],'member_id'=>MemberVo::instance()->getMemberId(),'is_admin'=>1]);
                $member_info = Member::get(MemberVo::instance()->getMemberId());
                $arr = [
                    'member_id'=>MemberVo::instance()->getMemberId(),
                    'member_mobile'=>$member_info['member_mobile'],
                    'member_avatar'=>$member_info['member_avatar'],
                    'member_name'=>$member_info['member_name'],
                    'mini_openid'=>$member_info['mini_openid'],
                    'default_shop_id'=>$params['shop_id'],
                    'token'=>MemberVo::instance()->getToken(),
                    'member_type'=>Member::MEMBER_TYPE_SHOP,
                    'role_id'=>1
                ];
                Member::update(['default_shop_id'=>$params['shop_id'],'member_type'=>Member::MEMBER_TYPE_SHOP],['id'=>MemberVo::instance()->getMemberId()]);
                Cache::store('redis')->set('member_token_'.MemberVo::instance()->getMemberId(),MemberVo::instance()->getToken(),604800); //token缓存
                Cache::store('redis')->rm(MemberVo::instance()->getToken());
                Cache::store('redis')->set(MemberVo::instance()->getToken(),json_encode($arr),604800); //保存用户信息
                return $arr;
            }
        }
        MemberApply::create(['member_id'=>MemberVo::instance()->getMemberId(),'shop_id'=>$params['shop_id'],'status'=>1]);
        return [];
    }







}